<?php

// +----------------------------------------------------------------------
// | Author: 逯佳 电影栏目 电视剧栏目 搜索栏目（其它栏目皆为搜索页面）演员或片名搜索栏目
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
class CatController extends Controller {

	public $artm;
	public $catm;
	public $typem;

	public function __construct(){
		parent::__construct();
		$this->artm = M('art');
		$this->catm = M('cat');
		$this->typem = M('type');
	}

	//电影
	public function movie(){
		$artm = $this->artm;
		$catm = $this->catm;

		//取出预告 以及总数
		$data['yg'] = $artm->field('art_id,title,img')->where(['cat_id'=>5])->order('update_time desc')->limit('0,8')->select();
		$data['ygsum'] = $artm->where(['cat_id'=>5])->count();
		
		//模块列表
		$data['classall'] = $catm->where(['parent_id'=>1])->select();

		//取出各类型最新影片
		foreach ($data['classall'] as $k => $v) {
			$data['new'][$k] = $artm->field('art_id,title,img,year')->where(['class'=>$v['cat_id']])->order('update_time desc')->limit('0,12')->select(); 
			$data['sum'][$k] = $artm->where(['class'=>$v['cat_id']])->count();
		}
		//取出各类型最火影片
		$time = strtotime('-7 date');
		foreach ($data['classall'] as $k => $v) {
			$data['hot'][$k] = $artm->field('art_id,title,update_time')->where(['class'=>$v['cat_id'],'update_tiem'=>['gt',$time]])->order('hot desc')->limit('0,14')->select(); 
		}
		$data['catname'] = '电影';
		$this->assign($data);
		$this->display('movie');
	}
	//电视剧
	public function tv(){
		$artm = $this->artm;
		$catm = $this->catm;

		//取出按照播放日排序的剧
		for ($i=0; $i < 7; $i++) { 
			$data['zb'][$i] = $artm->field('art_id,title,img,sum')->where(['play_time'=>$i+1,'status'=>0,'cat_id'=>2])->limit('0,3')->select();
		};	
		//子类集合 循环模块
		$data['classall'] = $catm->where(['parent_id'=>2])->select();

		//取出各类型最新影片
		foreach ($data['classall'] as $k => $v) {
			$data['new'][$k] = $artm->field('art_id,title,img,year')->where(['class'=>$v['cat_id']])->order('update_time desc')->limit('0,12')->select(); 
			$data['sum'][$k] = $artm->where(['class'=>$v['cat_id']])->count();
		}
		//取出各类型最火影片
		$time = strtotime('-7 date');
		foreach ($data['classall'] as $k => $v) {
			$data['hot'][$k] = $artm->field('art_id,title,update_time')->where(['class'=>$v['cat_id'],'update_tiem'=>['gt',$time]])->order('hot desc')->limit('0,14')->select(); 
		}
		$data['catname'] = '电视剧';
		$this->assign($data);
		$this->display('tv');
	}

	//根据搜索页面要求生成where条件
	public function createWhere(){
		if (!empty(I('cat_id'))) {
			$data['cat_id'] = I('cat_id');
		}
		if (I('type') != '全部') {
			$data['type'] = ['like','%'.I('type').'%'];
		}
		if (I('region') != '全部') {
			$data['region'] = I('region');
		}
		if (I('year') != '全部') {
			$data['year'] = I('year');
		}
		return $data;
	}
	/**
	 * 生成分页
	 * $count 文章总数
	 * $sum 页面显示数
	 */
	public function getPage($count,$sum){
		$Page = new \Think\Page($count,$sum);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		return $Page->show();// 分页显示输出
	}
	//搜索页面返回文章 
	//ajax返回
	public function getArt(){
		$artm = $this->artm;
		$where = $this->createWhere();
		$order = I('order').' desc';
		$start = I('page');
		$start = ($start-1)*4;
		$limit = $start.',4';
		//获取到的文章
		$data['artall'] = $artm->field('art_id,img,title,year,grade,actor,status,region,update_time,sum')->where($where)->order($order)->limit($limit)->select();	
		//获取到的文章总数
		$data['sum'] = $artm->where($where)->order($order)->count();
		//获取分页
		$data['page'] = $this->getPage($data['sum'],4);
		$this->ajaxReturn($data,'json');
	}
	//搜索页面条件以及页面的生成
	public function search(){
		$catm = $this->catm;
		$typem = $this->typem;
		//全部栏目
		$data['catall'] = $catm->where(['parent_id'=>0])->select();
		$cat_id = I('cat');
		//当前栏目
		if (empty($cat_id)) {
			$data['catname'] = '全部';
			//类型
			$data['typeall'] = $typem->select();
		}else{
			//子类
			$data['classall'] = $catm->where(['parent_id'=>$cat_id])->select();
			//类型
			$data['typeall'] = $typem->where(['cat_id'=>$cat_id])->select();
			$data['catname'] = $catm->where(['cat_id'=>$cat_id])->find()['catname'];
		}
		$this->assign($data);
		$this->display('search');
	}
	//根据片名或演员搜索取出文章
	//ajax返回
	public function nameGetArt(){
		$artm = $this->artm;
		$rsom = $this->rsom;
		$search = I('searchword');
		$data['searchword'] = I('searchword');
		$where = ['title'=>['like','%'.$search.'%']];
		$start = I('get.p');
		$start = ($start-1)*4;
		$limit = $start.',4';
		$data['artall'] = $artm->where($where)->order('update_time desc')->limit($limit)->select();
		if (empty($data['artall'])) {
			$where = ['actor'=>['like','%'.$search.'%']];
			$data['artall'] = $artm->where($where)->order('update_time desc')->limit($limit)->select();
		}
		//总数
		$data['sum'] = $artm->where($where)->count();
		//获取分页
		$data['page'] = $this->getPage($data['sum'],4);
		$this->ajaxReturn($data,'json');
	}
	//文章以及演员搜索页面
	public function artsearch(){
		$data['searchword'] = I('searchword');
		if (empty($data['searchword'])) {
			header('location:/index.php');
		}
		$data['catname'] = '片名或演员';
		$this->assign($data);
		$this->display('artsea');
	}

}

